package com.slkj.shilixiaoyuanapp.ui.common;

import android.annotation.SuppressLint;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.widget.ProgressBar;
import com.example.api.InjectParam;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.data.UserRequest;
import com.slkj.shilixiaoyuanapp.util.ListUtils;
import com.slkj.shilixiaoyuanapp.view.dialog.ShareDialog;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;
import butterknife.BindView;

@ActivityInfo(layout = R.layout.activity_web,rightHint = "")
public class WebActivity extends BaseActivity {
    @InjectParam
    String url;
    @InjectParam
    String title;
    @InjectParam
    int position;
    @BindView(R.id.web)
    WebView web;
    @BindView(R.id.progressBar2)
    ProgressBar progressBar;

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void init() {
        WebActivity__JumpCenter.bind(this);
        //setTitle(title);
        WebSettings webSettings = web.getSettings();
        webSettings.setSupportZoom(false);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSettings.setMixedContentMode(WebSettings.LOAD_NORMAL );
        }
        WebChromeClient chromeClient = new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView webView, int i) {
                if (progressBar == null)
                    return;
                progressBar.setProgress(i);
                if (i == 100){
                    progressBar.setVisibility(View.GONE);

                }else {
                    progressBar.setVisibility(View.VISIBLE);

                }
            }

            @Override
            public void onReceivedTitle(WebView webView, String s) {
//                setTitle(s);
            }
        };
        web.setWebChromeClient(chromeClient);
        web.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView webView, String s) {
                webView.loadUrl(s);// 当打开新链接时，使用当前的 WebView，不会使用系统其他浏览器
                return true;
            }

            @Override
            public void onPageFinished(WebView webView, String s) {
                //setTitle(webView.getTitle());
                int userid=UserRequest.getInstance().getUser().getUser().getId();
                int classid = -1;
                if (!ListUtils.isEmpty(UserRequest.getInstance().getUser().getClassX())){
                    classid=UserRequest.getInstance().getUser().getClassX().get(0).getClassId();
                }
                String roles= UserRequest.getInstance().getUser().getUser().getURole()+"";
                int studentid=UserRequest.getInstance().getUser().getUser().getId();
                String token=UserRequest.getInstance().getUser().getUserToken();
                int loginId=UserRequest.getInstance().getUser().getUser().getULoginUserId();
                int finalClassid = classid;
                runOnUiThread(new Runnable() {
                    @SuppressLint("LongLogTag")
                    @Override
                    public void run() {
                        if (android.os.Build.VERSION.SDK_INT < 19) {
                            web.loadUrl("javascript:GetRoles('"+userid+"','"+ finalClassid +"','"+title+"','"+roles+"','"+token+"','"+loginId+"','"+studentid+"')");
                            Log.e("============",userid+ finalClassid +title+roles+token+loginId+studentid);
                        }else{
                            web.evaluateJavascript("javascript:GetRoles('"+userid+"','"+ finalClassid +"','"+title+"','"+roles+"','"+token+"','"+loginId+"','"+studentid+"')", null);
                            Log.e("============",userid+ finalClassid +title+roles+token+loginId+studentid);
                        }
                    }
                });
            }

            @Override
            public void onReceivedSslError(WebView webView, com.tencent.smtt.export.external.interfaces.SslErrorHandler sslErrorHandler, com.tencent.smtt.export.external.interfaces.SslError sslError) {
                sslErrorHandler.proceed();
            }

        });
        web.loadUrl(url);
        web.addJavascriptInterface(new JSHook(), "slkj");
    }

    @Override
    public void onBackPressed() {
        if (web.canGoBack()){
            web.goBack();
        }else {
            finish();
        }
    }

    private class JSHook {
        @JavascriptInterface
        public void share(String title,String url,String img,String content){
            ShareDialog shareDialog=new ShareDialog();
            shareDialog.setUrl(url,title,content);
            shareDialog.show(getSupportFragmentManager());
        }
        @JavascriptInterface
        public void GetTitle(String title){
            setTitle(title);
        }

        @JavascriptInterface
        public void TokenError(String err){

        }
    }
}
